package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.service.IStrateCommentService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("strategyComment")
public class StrategyCommentController {
    @Autowired
    private IStrateCommentService strateCommentService;
    @Autowired
    private IStrategyService strategyService;

    // 查询所有的攻略点评

    /**
     * 1.分页
     * 2.高级查询(根据攻略标题和攻略点评状态)
     *
     */
    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") StrategyCommentQueryObject qo){
        // 获取所有的攻略
        model.addAttribute("strategies",strategyService.selectAll());

        PageInfo pageInfo = strateCommentService.query(qo);
        model.addAttribute("result",pageInfo);
        return "strategyComment/list";
    }

    @RequestMapping("getImgUrlAndContent")
    @ResponseBody
    public Object getImgUrlAndContent(Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        StrategyComment strategyComment = strateCommentService.getStrategyCommentById(id);
        // 获取当前点评内容
        String content = strategyComment.getContent();
        map.put("content",content);
        // 获取图片,要切割字符串
        String imgUrls = strategyComment.getImgUrls();
        String[] imgUrlsArr = imgUrls.split(";");
        map.put("imgUrlsArr",imgUrlsArr);
        return map;
    }
    // 修改攻略点评的状态和头条推荐时间
    @RequestMapping("update")
    @ResponseBody
    public Object update(StrategyComment strategyComment){
        JSONResult result = new JSONResult();
        try {
            strateCommentService.update(strategyComment);
        }catch (Exception e){
            e.getMessage();
            result.mark(e.getMessage());
        }
        return result;
    }

}
